<template>
	<om-addpage :busCode="busCode" style="padding-top: 35px;" :menuName="运营指标填报表" @formSubmit="formSubmit" :showHistoryBtn="true">
		<uni-list>
			<uni-list-item :showArrow="!checkedit">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="制表岗位" required></om-label></view>
					<view v-if="checkedit" class="tui-right tui-col-8">{{ formdata.zposName }}</view>
					<view v-else class="tui-right tui-col-8">
						<picker mode="selector" @change="zposFunc" :range="zposList" :value="zposIndex" range-key="posname">
							<view>{{ zposList.length ? zposList[zposIndex].posname : '请选择' }}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item :showArrow="!checkedit">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="统计日期" required></om-label></view>
					<view v-if="checkedit" class="tui-right tui-col-8">{{ formdata.reportTime }}</view>
					<view v-else class="tui-right tui-col-8">
						<picker mode="date" @change="reportTimeChange" v-model="formdata.reportTime" :value="formdata.reportTime">
							<view>{{ formdata.reportTime.trim() ? formdata.reportTime : '请选择' }}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item :showArrow="!checkedit">
				<view class="tui-flex">
					<view class="tui-col-5"><om-label label="指标承担人" required></om-label></view>
					<view v-if="checkedit" class="tui-right tui-col-7">{{ formdata.postUserName }}</view>
					<view v-else class="tui-right tui-col-7" @click="postUserFunc">
						<view>{{ formdata.postUserName || '请选择' }}</view>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-5"><om-label label="指标承担机构"></om-label></view>
					<view class="tui-col-7">{{ formdata.modelInstName }}</view>
				</view>
			</uni-list-item>
		</uni-list>
		<om-white-space bgColor="#eee"></om-white-space>
		<om-sub title="明细">
			<view slot="extra">{{ dataList.length ? '点击项目可进行编辑' : '' }}</view>
			<uni-list v-for="(item, index) in dataList" :key="index">
				<uni-list-item :showArrow="true" @click="openData(index)">
					<view>{{ item.indexDictName + (item.indexCategoryName ? ' | ' + item.indexCategoryName : '') }}</view>
					<view class="text-gray text-sm padding-bottom-xs padding-top-xs">
						{{ item.userName + ' | ' + item.postName + ' | ' + (item.sum || item.sum === 0 ? item.sum : '') + item.indexUnitName }}
					</view>
					<view class="text-gray text-sm">{{ item.remark }}</view>
				</uni-list-item>
			</uni-list>
			<view v-if="!checkedit" class="tui-flex align-center justify-center padding-top"><text class="lg text-green cuIcon-add" @click="openData()">增加明细</text></view>
		</om-sub>
		<om-white-space bgColor="#eee"></om-white-space>
	</om-addpage>
</template>

<script>
import { loadZpos } from '../../../static/js/formOld.js';
import { getUrl, isEmpty, confirmModal } from '../../../static/js/common.js';
import dateTimePicker from '@/static/js/dateTimePicker.js';
import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
const form = require('../../../static/js/formValidation.js');
export default {
	data() {
		return {
			checkedit: false,
			formdata: {
				id: '',
				zuserId: '',
				zuserName: '',
				zposId: '',
				zposName: '',
				reportYear: '',
				reportMonth: '',
				reportTime: '',
				postUserId: '',
				postUserName: '',
				modelUserId: '',
				modelUserName: '',
				modelPostId: '',
				modelPostName: '',
				modelInstId: '',
				modelInstName: '',
				dataStr: '' //指标明细
			},
			zposIndex: 0,
			zposList: [],
			postUserIndex: -1,
			postUserList: [],
			dataList: [],
			postUserCacheKey: 'ckd-zb-AnyTimeReporte-postUser-cache',
			dataCacheKey: 'ckd-zb-AnyTimeReporte-data-info-cache',
			menuName: '运营指标填报表',
			id: '',
			busCode: 'DTM0201'
		};
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	watch: {
		'formdata.reportTime'(newValue, oldValue) {
			let _that = this;
			if (newValue) {
				_that.formdata.reportYear = new Date(newValue).getFullYear();
				_that.formdata.reportMonth = new Date(newValue).getMonth() + 1;
			} else {
				_that.formdata.reportYear = '';
				_that.formdata.reportMonth = '';
			}
		},
		dataList(newValue, oldValue) {
			let _that = this;
			if (newValue && newValue.length) {
				_that.formdata.dataStr = JSON.stringify(newValue);
			} else {
				_that.formdata.dataStr = '';
			}
		}
	},
	methods: {
		/**
		 * 初始化
		 */
		initLoad() {
			let _that = this;
			loadZpos(_that);
			// _that.formdata.reportTime = dateTimePicker.getNewDate();
		},
		/**
		 * 制表岗位
		 */
		zposFunc(e) {
			//清空明细数据
			this.reportTime = '';
			this.formdata.postUserId = '';
			this.formdata.postUserName = '';
			this.formdata.modelUserId = '';
			this.formdata.modelUserName = '';
			this.formdata.modelPostId = '';
			this.formdata.modelPostName = '';
			this.formdata.modelInstId = '';
			this.formdata.modelInstName = '';
			this.dataList = [];
			if (e) {
				if (this.zposList.length && e.target.value == -1) {
					this.zposIndex = 0;
				} else {
					this.zposIndex = e.target.value;
				}
			}
			if (~this.zposIndex && this.zposList.length) {
				this.formdata.zposId = this.zposList[this.zposIndex].id;
				this.formdata.zposName = this.zposList[this.zposIndex].posname;
			} else {
				this.formdata.zposId = '';
				this.formdata.zposName = '';
			}
		},
		/**
		 * 统计日期
		 */
		reportTimeChange(e) {
			//清空明细数据
			this.formdata.postUserId = '';
			this.formdata.postUserName = '';
			this.formdata.modelUserId = '';
			this.formdata.modelUserName = '';
			this.formdata.modelPostId = '';
			this.formdata.modelPostName = '';
			this.formdata.modelInstId = '';
			this.formdata.modelInstName = '';
			this.dataList = [];
			if (e) {
				this.formdata.reportTime = e.detail.value;
			} else {
				this.formdata.reportTime = '';
			}
		},

		//指标承担人 点击事件
		postUserFunc(e) {
			let url = getUrl('/pages/public/pageSearch/pageSearch', {
				cachename: this.postUserCacheKey,
				initload: true,
				namefield: 'userPosName',
				notefield: 'instName',
				url: 'orgInstPosUserOfficial/findPagerByOrgId',
				searchfield: 'q'
			});

			uni.navigateTo({
				url: url
			});
		},

		loadSubData() {
			let _that = this;
			let formdata = _that.formdata;
			_that.dataList = [];
			if (formdata.zposId && formdata.reportTime && formdata.modelUserId && formdata.modelPostId) {
				var param = {
					zposId: formdata.zposId,
					reportTime: formdata.reportTime,
					userId: formdata.modelUserId,
					postId: formdata.modelPostId,
					dataStr: '[]'
				};
				confirmModal(
					_that,
					'提示信息',
					'是否赋值承担人所有未填报的计划项指标？',
					function() {
						_that.om.request('anyTimeReport/findModeList', param, 'post').then(res => {
							if (res.succeed) {
								_that.dataList = res.value;
							} else {
								_that.om.toast(res.msg);
							}
						});
					},
					function() {
						return false;
					}
				);
			}
		},

		openData(index) {
			uni.setStorage({
				key: this.dataCacheKey,
				data: this.dataList
			});
			let zposId = this.formdata.zposId;
			let reportTime = this.formdata.reportTime;
			let modelUserId = this.formdata.modelUserId;
			let modelUserName = this.formdata.modelUserName;
			let modelPostId = this.formdata.modelPostId;
			let modelPostName = this.formdata.modelPostName;
			let modelInstId = this.formdata.modelInstId;
			let modelInstName = this.formdata.modelInstName;
			if (reportTime && zposId) {
				let param = {
					index: index,
					checkedit: this.checkedit ? true : '',
					cachename: this.dataCacheKey,
					reportTime: reportTime,
					zposId: zposId,
					modelUserId: modelUserId,
					modelUserName: modelUserName,
					modelPostId: modelPostId,
					modelPostName: modelPostName,
					modelInstId: modelInstId,
					modelInstName: modelInstName
				};
				//编辑明细
				let url = getUrl('/pages/form/ckd-zb-anytimereport/data-info', param);
				uni.navigateTo({
					url: url
				});
			} else {
				this.om.toast('请先选择制表岗位、统计日期');
			}
		},
		formSubmit() {
			let _that = this;
			_that.formdata.zuserId = this.userId;
			_that.formdata.zuserName = this.userName;
			var tipArray = [];
			var dataList = _that.dataList;
			for (let i = 0; i < dataList.length; i++) {
				if (dataList[i].indexDictId) {
					var obj = dataList[i];
					if (isEmpty(obj.sum)) {
						tipArray.push(i + 1);
					}
					if (obj.indexCategoryRequired && !obj.indexCategoryName) {
						one_button('填报指标信息，第' + i + '行运营指标范围为空');
						return;
					}
				}
			}
			if (tipArray.length) {
				var str = tipArray.join('、');
				var tip = '填报指标信息，第' + str + '行本人执行数为空，在审批后将做 0 处理，是否继续保存？';
				confirmModal(
					_that,
					'提示信息',
					tip,
					function() {
						_that.saveData();
					},
					function() {
						return false;
					}
				);
			} else {
				_that.saveData();
			}
		},

		saveData() {
			let _that = this;
			//表单规则
			let rules = [
				{
					name: 'zposId',
					rule: ['required'],
					msg: ['请选择制表岗位']
				},
				{
					name: 'reportTime',
					rule: ['required'],
					msg: ['请选择统计日期']
				},
				{
					name: 'postUserId',
					rule: ['required'],
					msg: ['请选择指标承担人']
				},
				{
					name: 'dataStr',
					rule: ['required'],
					msg: ['请添加明细']
				}
			];
			var formdata = {};
			for (var i in _that.formdata) {
				if (_that.formdata[i]) {
					formdata[i] = _that.formdata[i];
				}
			}

			let checkRes = form.validation(formdata, rules);
			if (!checkRes) {
				if (this.checkedit) {
					// _that.om.formSave('anyTimeReport/doReviewUpdate', formdata, _that, function(v){
					// 	uni.removeStorage({
					// 		key:_that.dataCacheKey
					// 	})
					// 	_that.om.getFormInfo(formdata.id, _that.busCode);
					// })
				} else {
					_that.om.formSave('anyTimeReport/doAdd', formdata, _that, function(v) {
						uni.removeStorage({
							key: _that.dataCacheKey
						});
						_that.om.getFormInfo(v, _that.busCode);
					});
				}
			} else {
				_that.om.toast(checkRes);
			}
		},

		loadData(id) {
			let _that = this;
			this.om.request('anyTimeReport/getValue', { fileId: id }).then(res => {
				if (res.succeed) {
					_that.formdata = res.value;
					_that.dataList = res.value.dataList;
					delete _that.formdata.dataList;
					uni.setStorage({
						key: _that.dataCacheKey,
						data: _that.dataList
					});
				} else {
					_that.om.toast(res.msg);
				}
			});
		}
	},
	onLoad: function(options) {
		let _that = this;
		_that.menuName = options.title || '运营指标填报表';
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		if (options.checkedit) {
			// this.checkedit = true
			// if(!options.fileId){
			// 	_that.om.toast("没有找到文件ID")
			// 	return
			// }
			// _that.loadData(options.fileId);
		} else {
			_that.initLoad();
		}
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		let _that = this;
		uni.getStorage({
			key: _that.dataCacheKey,
			success: function(res) {
				_that.dataList = res.data;
				uni.removeStorage({
					key: _that.dataCacheKey
				});
			}
		});
		uni.getStorage({
			key: _that.postUserCacheKey,
			success: function(res) {
				let data = res.data;
				_that.formdata.postUserId = data.id;
				_that.formdata.postUserName = data.userPosName;
				_that.formdata.modelUserId = data.userId;
				_that.formdata.modelUserName = data.userName;
				_that.formdata.modelPostId = data.posId;
				_that.formdata.modelPostName = data.posName;
				_that.formdata.modelInstId = data.instId;
				_that.formdata.modelInstName = data.instName;
				_that.loadSubData();
				uni.removeStorage({
					key: _that.postUserCacheKey
				});
			}
		});
	}
};
</script>

<style></style>
